<template>
  <el-dialog title="新增部门" :visible.sync="dialogDat">
    <el-form :model="form" ref="myform" label-width="100px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入3-5个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="请输入8-11个编码"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择">
          <el-option
            v-for="(item, index) in list"
            :key="index"
            :label="item.username"
            :value="item.username"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          placeholder="请输入1-300个字符"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="sabumit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
name: 'add-dept'
import { deptApi, addDataApi, getDeptDetailApi } from '@/api/departments'
import { userGetApi } from '@/api/elmentyess'
export default {
  props: ['dialogDat', 'id'],
  data () {
    return {
      list: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
          {
            validator: async (rule, value, callback) => {
              let res = await deptApi()
              if (!this.form.id) {
                res.data.depts.some(item => {
                  return item.pid === this.id && item.name == value
                })
                  ? callback(new Error('改名字已经被占用'))
                  : callback()
              } else {
                res.data.depts.some(
                  item =>
                    item.pid === this.form.pid &&
                    item.id !== this.form.id &&
                    item.name === value
                )
                  ? callback(new Error('部门名不能起冲突'))
                  : callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
          {
            validator: async (rule, value, callback) => {
              let res = await deptApi()
              if (!this.form.id) {
                res.data.depts.some(item => {
                  return item.code == value
                })
                  ? callback(new Error('改名字已经被占用'))
                  : callback()
              } else {
                res.data.depts.some(
                  item =>
                    item.pid === this.form.pid &&
                    item.id !== this.form.id &&
                    item.name === value
                )
                  ? callback(new Error('部门编码不能起冲突'))
                  : callback()
              }
            },
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          {
            min: 3,
            max: 300,
            message: '长度在 3 到 300 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    sabumit () {
      this.$refs.myform.validate(async res => {
        if (res) {
          this.$emit('upDateData')
          this.$message = {
            type: 'success',
            message: '新增成功'
          }
          await addDataApi({
            ...this.form,
            pid: this.id
          })
          this.form = {
            name: '',
            code: '',
            manager: '',
            introduce: ''
          }
          this.$emit('upDate')
          console.log('通过')
        } else {
          console.log('有毛病')
        }
      })
    },
    async getDeptDetail (id) {
      let res = await getDeptDetailApi(id)
      this.form = res.data
      console.log(res)
    },
    // cancel () {
    //   console.log(111)
    //   this.$emit('update:dateShow', false)
    // }
    cancel () {
      this.$emit('update:dialogDat', false)
    }
  },
  async created () {
    let res = await userGetApi()
    this.list = res.data
    // console.log(this.list)
  }
}
</script>

<style></style>
